package week02.validator_ex;

import java.io.FileInputStream;
import java.io.IOException;
import java.util.Properties;

public class AuthenticatorFactory {
    private static Properties prop = new Properties();
    static {
        try {
            prop.load(new FileInputStream("src/week02/validator_ex/auth.properties"));
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static Authenticator createAuthenticator() {
        KeyValueKeeper keeper=createKeyValueKeeper();
        Encrypter encrypter=createEncrypter();
        return new Authenticator(keeper,encrypter);
    }

    private static KeyValueKeeper createKeyValueKeeper() {
        MemoryKeeper memoryKeeper = null;
        try {
            memoryKeeper = (MemoryKeeper) Class.forName(prop.getProperty("KeyValueKeeper")).newInstance();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return memoryKeeper;
    }

    private static Encrypter createEncrypter() {
        Md5Encrypter encrypter = null;
        try {
            encrypter = (Md5Encrypter) Class.forName(prop.getProperty("Encrypter")).newInstance();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return encrypter;
    }
}